package com.example.routeplansystem.utils;

import com.example.routeplansystem.exception.ServiceException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * @author 唐海军
 * @create 2022-08-07 17:38
 */

@AllArgsConstructor
@ApiModel
public class PagingUtil<T> {
    @ApiModelProperty("当前页")
    private int currentPage;

    @ApiModelProperty("每页数量")
    private int counts;

    @ApiModelProperty("需要分页的数据")
    private List<T> datas;

    public  List<T> getPagingData(){
        if(currentPage<1||counts<1)throw new ServiceException("当前页数和每页数量不能小于1");
        Integer start=(currentPage-1)*counts;
        if(start<0||start>datas.size())throw new ServiceException("当前页没有数据了");
        Integer end =currentPage*counts;
        if(end>datas.size())end=datas.size();
        return datas.subList(start,end);
    }
}
